package com.tanhua.domain.mongo;

import cn.hutool.core.bean.BeanUtil;
import com.tanhua.domain.vo.OptionsVo;
import com.tanhua.domain.vo.QuestionsVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "soul_question")
public class Questions implements Serializable{
    private ObjectId id;//试题编号
    private ObjectId SoulId;//试卷id
    private String question;//题目
    private List<Options> options;//选项列表

    public void addOptions(String options) {
        String[] tempOptions = options.split(" ");
        List<Options> optionsList = new ArrayList<>();
        for (String tempOption : tempOptions) {
            Options options1 = new Options();
            options1.setId(new ObjectId());
            options1.setQuestionsId(this.id);
            options1.setOption(tempOption);
            optionsList.add(options1);
        }
        setOptions(optionsList);
    }

    public List<OptionsVo> getOptionsVo(List<Options> options) {
        List<OptionsVo> voList = new ArrayList<>();
        for (Options option : options) {
            OptionsVo optionsVo = new OptionsVo();
            BeanUtil.copyProperties(option,optionsVo);
            optionsVo.setId(option.getId().toHexString());
            optionsVo.setQuestionsId(option.getQuestionsId().toHexString());
            voList.add(optionsVo);
        }
        return voList;
    }
}

